//
//  TimeViewController.m
//  ForeverLove
//
//  Created by lanou3g on 15/11/13.
//  Copyright © 2015年 文萱竹. All rights reserved.
//

#import "TimeViewController.h"
#import "MyTableHeaderView.h"

// 设置Button的宽度（Button是正方形，所有宽高相等）
#define kButtonWidth 50.0
// 设置Button的Y坐标
#define kButtonY 280.0

#define kNaviBarHeight 64.0
#define kTabBarHeight 44.0

@interface TimeViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, MyTableHeaderViewDelegate>

@property (strong, nonatomic) IBOutlet UIBarButtonItem *leftItem;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *rightItem;

- (IBAction)leftItemClickedAction:(UIBarButtonItem *)sender;
- (IBAction)rightItemClickedAction:(UIBarButtonItem *)sender;

@property (strong, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) IBOutlet UIScrollView *headerScrollView;

@property (strong, nonatomic) IBOutlet UIImageView *headerImageView;


@property (nonatomic, assign) BOOL isHidden;

@property (nonatomic, strong) UIButton *textButton;
@property (nonatomic, strong) UIButton *pictureButton;
@property (nonatomic, strong) UIButton *shareButton;

@property (nonatomic, strong) NSMutableArray *backgroundImageArray;

@property (strong, nonatomic) IBOutlet UIImageView *oneHeaderImage;
@property (strong, nonatomic) IBOutlet UIImageView *twoHeaderImage;

@property (nonatomic, strong) NSMutableDictionary *sectionDict;

@end

@implementation TimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 存背景图片的数组
    self.backgroundImageArray = [NSMutableArray arrayWithObjects:@"one.jpg", @"two.jpg", @"three.jpg", nil];
    [self.leftItem setImage:[UIImage imageNamed:@"time_nav_toggle_img_ablum"]];
    
    
    [self loadTableView];
    [self editTableHeaderView];
}

// 设置tableView相关的属性
- (void)loadTableView
{
    // 设置cell的高度
    [self.tableView setRowHeight:80.0f];
    [self.tableView setSectionHeaderHeight:50.0];
    // 设置tableHeaderView的高度
    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, 0, 160);
    //    // 取消cell中间线
    //    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

// 编辑tableHeaderView的内容
- (void)editTableHeaderView
{
    self.headerScrollView.userInteractionEnabled = NO;
    self.headerImageView.image = [UIImage imageNamed:@"one.jpg"];
    self.headerScrollView.contentSize = self.headerImageView.frame.size;
    // 设置定时器 —— 设置滚动视图按时间滚动
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollAction:) userInfo:nil repeats:YES];
    
    _oneHeaderImage.layer.masksToBounds = YES;
    _oneHeaderImage.layer.cornerRadius = 20.0;
    _twoHeaderImage.layer.masksToBounds = YES;
    _twoHeaderImage.layer.cornerRadius = 20.0;
    
}

#warning  ---- 图片更替之间加个过渡动画 ----
// 计时器事件 -- 让图片上下滚动
- (void)scrollAction:(NSTimer *)sender
{
    static CGFloat offsetY = 2.0; // 每单位时间偏移量
    // 1. 设置scrollView滚动
    [self.headerScrollView setContentOffset:CGPointMake(0, offsetY) animated:YES];
    offsetY += 2.0;
    
    // 2. 判断scrollView滚动是否到了边界
    if (self.headerScrollView.contentOffset.y >= self.headerScrollView.frame.size.height) {
        
        // 3. scrollView回到起始点
        self.headerScrollView.contentOffset = CGPointMake(0, 0);
        offsetY = 0.0;
        
        // 4. 更换imageView上的图片
        static NSInteger i = 1; // i = 1 , backgroundImageArray.count = 3 <0,1,2>
        
        if (i < self.backgroundImageArray.count) {
            self.headerImageView.image = [UIImage imageNamed:_backgroundImageArray[i]];
            i ++;
        } else {
            i = 0;
            self.headerImageView.image = [UIImage imageNamed:_backgroundImageArray[i]];
            i ++;
        }
    }
}

////去掉UItableview headerview黏性
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    if (scrollView == self.tableView)
//    {
//        CGFloat sectionHeaderHeight = 50.0;
//        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
//            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
//        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
//            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//        }
//    }
//}

#pragma mark --- tablView 的代理方法 ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    
    cell.textLabel.text = [NSString stringWithFormat:@"我是第%ld个cell", indexPath.row];
//    // 可以设置cell点击状态下的颜色
//    UIView *view = [[UIView alloc]init];
//    view.backgroundColor = [UIColor blueColor];
//    cell.selectedBackgroundView = view;
//    // 附件样式
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *headerViewID = @"myID";
    
    MyTableHeaderView *headrView = self.sectionDict[@(section)];
    
    if (headrView == nil) {
        
        headrView = [[MyTableHeaderView alloc] initWithReuseIdentifier:headerViewID];
        headrView.delegate = self;
        
        [self.sectionDict setObject:headrView forKey:@(section)];
    }
    
    headrView.section = section;
    [headrView.detailBtn setTitle:[NSString stringWithFormat:@"section:%ld", section] forState:UIControlStateNormal];
    
    return headrView;
    
}

// 实现代理方法
- (void)myHeaderViewDidSelected:(MyTableHeaderView *)headerView
{
    NSLog(@"%ld", headerView.section);
}

#pragma mark --- 左上角 item 点击事件 ---
- (IBAction)leftItemClickedAction:(UIBarButtonItem *)sender
{
    if (self.isHidden) {
        [self.leftItem setImage:[UIImage imageNamed:@"time_nav_toggle_img_ablum"]];
        self.isHidden = NO;
    } else {
        [self.leftItem setImage:[UIImage imageNamed:@"time_nav_toggle_img_list"]];
        self.isHidden = YES;
    }
}

// 轻拍手势把Button从view上移除
- (void)removeButton:(UITapGestureRecognizer *)sender
{
    // 移除Button
    [_textButton removeFromSuperview];
    [_pictureButton removeFromSuperview];
    [_shareButton removeFromSuperview];
    _textButton = nil;
    _pictureButton = nil;
    _shareButton = nil;
    
    // 移除view
    UIView *view = (UIView *)[[UIApplication sharedApplication].keyWindow viewWithTag:2000];
    [view removeFromSuperview];
    view = nil;
    
    // 移除轻拍事件
    [[UIApplication sharedApplication].keyWindow removeGestureRecognizer:sender];
    sender = nil;
    
}

#pragma mark --- 右上角 item 点击事件 ---
- (IBAction)rightItemClickedAction:(UIBarButtonItem *)sender
{
    sender.enabled = NO; // 设置按钮不可点击（执行完一下代码恢复激活状态）
    // 初始化一个轻拍事件，添加到window上
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeButton:)];
    [[UIApplication sharedApplication].keyWindow addGestureRecognizer:tapGesture];
    
    if (_textButton == nil) {
        
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height + kNaviBarHeight + kTabBarHeight)];
        view.tag = 2000;
        view.alpha = 0.0;
        [[UIApplication sharedApplication].keyWindow addSubview:view];
        __weak typeof(self) tSelf = self;
        // 设置view动画
        [UIView animateWithDuration:1.0 animations:^{
            
            view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + kNaviBarHeight);
            view.backgroundColor = [UIColor grayColor];
            view.alpha = 0.5;
            
        } completion:^(BOOL finished) {
            // 初始化textButton
            tSelf.textButton = [UIButton buttonWithType:UIButtonTypeCustom];
            tSelf.textButton.frame = CGRectMake(self.view.frame.size.width - 30, - 20, 0, 0);
            tSelf.textButton.backgroundColor = [UIColor whiteColor];
            tSelf.textButton.layer.cornerRadius = 5.0;
            tSelf.textButton.tag = 3000;
            [tSelf.textButton setBackgroundImage:[UIImage imageNamed:@"time_textbtn_cover_normal"] forState:UIControlStateNormal];
            [tSelf.textButton setBackgroundImage:[UIImage imageNamed:@"time_textbtn_cover_highlighted"] forState:UIControlStateHighlighted];
            [tSelf.textButton addTarget:self action:@selector(clickedAction:) forControlEvents:UIControlEventTouchUpInside];
            [[UIApplication sharedApplication].keyWindow addSubview:tSelf.textButton];
            
            // 初始化pictureButton
            tSelf.pictureButton = [UIButton buttonWithType:UIButtonTypeCustom];
            tSelf.pictureButton.frame = CGRectMake(self.view.frame.size.width - 30, - 20, 0, 0);
            tSelf.pictureButton.backgroundColor = [UIColor whiteColor];
            tSelf.pictureButton.layer.cornerRadius = 5.0;
            tSelf.pictureButton.tag = 3100;
            [tSelf.pictureButton setBackgroundImage:[UIImage imageNamed:@"time_picbtn_cover_normal"] forState:UIControlStateNormal];
            [tSelf.pictureButton setBackgroundImage:[UIImage imageNamed:@"time_picbtn_cover_highlighted"] forState:UIControlStateHighlighted];
            [tSelf.pictureButton addTarget:self action:@selector(clickedAction:) forControlEvents:UIControlEventTouchUpInside];
            [[UIApplication sharedApplication].keyWindow addSubview:tSelf.pictureButton];
            
            // 初始化shareButton
            tSelf.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
            tSelf.shareButton.frame = CGRectMake(self.view.frame.size.width - 30, - 20, 0, 0);
            tSelf.shareButton.backgroundColor = [UIColor whiteColor];
            tSelf.shareButton.layer.cornerRadius = 5.0;
            tSelf.shareButton.tag = 3200;
            [tSelf.shareButton setBackgroundImage:[UIImage imageNamed:@"time_sharebtn_cover_normal"] forState:UIControlStateNormal];
            [tSelf.shareButton setBackgroundImage:[UIImage imageNamed:@"time_sharebtn_cover_highlighted"] forState:UIControlStateHighlighted];
            [tSelf.shareButton addTarget:self action:@selector(clickedAction:) forControlEvents:UIControlEventTouchUpInside];
            [[UIApplication sharedApplication].keyWindow addSubview:tSelf.shareButton];
            
            // 加个动画改变Button的frame
            [UIView animateWithDuration:1.0f animations:^{
                tSelf.textButton.frame = CGRectMake(self.view.center.x - kButtonWidth / 2, kButtonY, kButtonWidth, kButtonWidth);
                tSelf.pictureButton.frame = CGRectMake(tSelf.textButton.frame.origin.x - kButtonWidth - 40, kButtonY, kButtonWidth, kButtonWidth);
                tSelf.shareButton.frame = CGRectMake(tSelf.textButton.frame.origin.x + kButtonWidth + 40, kButtonY, kButtonWidth, kButtonWidth);
            }];
            sender.enabled = YES; // 让按钮回复激活可点击状态
        }];
        
    }
}

// 三个按钮的点击事件
- (void)clickedAction:(UIButton *)sender
{
    switch (sender.tag) {
        case 3000:
            NSLog(@"文字");
            
            break;
        case 3100:
            NSLog(@"选照片");
            break;
        case 3200:
            NSLog(@"分享");
            break;
            
        default:
            break;
    }
    
}









/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */





@end
